package com.example.demo.util;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2019-06-05
 */
@Slf4j
public class FileManager {

    private List<File> files = new ArrayList<>();

    public List<File> getFiles(String source, int maxCount) {
        List<File> list = new ArrayList<>();
        Path path = Paths.get(source);
        try {
            DirectoryStream<Path> pathFilter = Files.newDirectoryStream(path);
            for (Path p : pathFilter) {
                File f = p.toFile();
                if (f.isDirectory()) {
                    list.addAll(getFiles(f.getAbsolutePath(), maxCount - list.size()));
                } else {
                    if (files.contains(f)) {
                        continue;
                    }
                    if (System.currentTimeMillis() - f.lastModified() < 10000) {
                        continue;
                    }
                    files.add(f);
                    list.add(f);
                }
                if (list.size() >= maxCount) {
                    break;
                }
            }
            pathFilter.close();
            files.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据参数清理文件夹
     *
     * @param rootPath 根目录
     * @param keepDays 保存日期
     */
    public void deleteFolderByParam(String rootPath, int keepDays) {
        try {
            String currentDay = DateUtil.format(DateUtil.offsetDay(new Date(), -keepDays), "yyyyMMdd");
            int currentDayInt = Integer.parseInt(currentDay);
            File[] devPathArr = new File(rootPath).listFiles();
            if (devPathArr != null && devPathArr.length > 0) {
                for (File devPath : devPathArr) {
                    if (devPath.isDirectory()) {
                        File[] dayPathArr = devPath.listFiles();
                        if (dayPathArr != null && dayPathArr.length > 0) {
                            for (File dayPath : dayPathArr) {
                                if (dayPath.isDirectory()) {
                                    int dirName = Integer.parseInt(dayPath.getName());
                                    if (dirName < currentDayInt) {
                                        File[] files = dayPath.listFiles();
                                        if (files != null && files.length > 0) {
                                            for (File file : files) {
                                                FileUtils.delete(file);
                                                log.info("deleteFolder[{}]file[{}]", dirName, file.getName());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("deleteFolderByParam Failed! [{}]", e.getMessage());
        }
    }

}
